/**
 * 将时间戳转换为时间
 * @param timeStamp
 * @returns
 */
function timeStampToDate(now) {
    if (now == null || now == "" || now == undefined) {
        return;
    }
    now = new Date(now);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    month = month >= 10 ? month : "0" + month;
    var date = now.getDate();
    date = date >= 10 ? date : "0" + date;
    var hour = now.getHours();
    hour = hour >= 10 ? hour : "0" + hour;
    var minute = now.getMinutes();
    minute = minute >= 10 ? minute : "0" + minute;
    var second = now.getSeconds();
    second = second >= 10 ? second : "0" + second;
    return year + "-" + month + "-" + date + "   " + hour + ":" + minute + ":"
        + second;
}

//将字符串转换为时间
function convertTime(timeStr) {
    return new Date(Date.parse(timeStr.replace(/-/g, "/")));
}

/**
 * js获取 XXXX-XX-XX 年-月-日
 * @returns {String}
 */
function getYMD() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    month = month >= 10 ? month : "0" + month;
    var date = now.getDate();
    date = date >= 10 ? date : "0" + date;
    return year + "-" + month + "-" + date;
}

/**
 *
 * @param 获取今天时相隔时间 'AddDayCount
 * document.write("前天："+GetDateStr(-2));
 * document.write("<br />昨天："+GetDateStr(-1));
 * document.write("<br />今天："+GetDateStr(0));
 * document.write("<br />明天："+GetDateStr(1));
 * document.write("<br />后天："+GetDateStr(2));
 * document.write("<br />大后天："+GetDateStr(3));
 * @returns {String}
 */
function getSpaceDate(spaceDate) {
    var dd = new Date();
    dd.setDate(dd.getDate() + spaceDate);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期
    var d = dd.getDate();
    return y + "-" + m + "-" + d;
}

//格式化时间
function formatTime(time, format) {
    var t = new Date(time);
    var tf = function (i) {
        return (i < 10 ? '0' : '') + i
    };
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
        switch (a) {
            case 'yyyy':
                return tf(t.getFullYear());
                break;
            case 'MM':
                return tf(t.getMonth() + 1);
                break;
            case 'mm':
                return tf(t.getMinutes());
                break;
            case 'dd':
                return tf(t.getDate());
                break;
            case 'HH':
                return tf(t.getHours());
                break;
            case 'ss':
                return tf(t.getSeconds());
                break;
        }
    })
}

